Technote 1184FCBs, Now and ForeverBy Quinn “The Eskimo!” |
CONTENTSIntroduction |
Mac OS 9.0 changes the format of the File Control Block (FCB) table significantly. This technote explains the original format of the FCB table, how the use of the FCB table has evolved over time, and how you can access FCB information in a compatible way. All Mac OS developers should read the Concrete Advice section to ensure that their software is compatible with Mac OS 9.0 and beyond. If your software is not compatible with Mac OS 9.0
(specifically, it causes a system error The other sections of the technote are background material for the Mac OS archeologists out there. |
IntroductionPrior to Mac OS 9.0, the Macintosh was limited to 348 simultaneously open files (FCBs). This limit proved to be a problem for both users and developers, so one of the design goals of Mac OS 9.0 was to increase this limit significantly. Everything has its price, however, and the price of increasing the number of FCBs is compatibility. Mac OS has used the same FCB table format since the introduction of HFS (1986) and a number of developers have (erroneously) grown dependent on this format.
This technote describes the changes to the FCB table format over time, and the consequences of these changes to developers. The full story is quite involved, and so the technote starts with some concrete advice for those folks with more legacy code than time. Concrete AdviceMac OS 9.0 is not the ultimate evolution of the FCB table. Apple expects to make further changes as the system evolves. You should look at your code now to strive for compatibility in the future. One specific thing to check is your use of file reference
numbers. File reference numbers are defined to be positive
For application-level software (applications, shared libraries, and so on), the best road to future compatibility is Carbon. The Carbon programming interfaces are specifically designed to be supportable on future systems. If you are programming for Carbon, you should be isolated from future, low-level File Manager changes. Specifically, if you want to access FCB
information from application-level code, you should use the
File Manager routine There are, however, circumstances in which it is not possible to call the File Manager to get FCB information. For example, if you are writing a File System Manager (FSM) plug-in, or you are writing a system extension and your code is executing within the context of a File Manager patch. In these circumstances, you may need to access the FCB immediately (that is, without queuing a File Manager request). If you need to access an FCB immediately and FSM is available, you should use the FSM accessor routines. These routines are discussed later in this technote. The only time it is acceptable to access the FCB table directly is when you need immediate access to the FCB and FSM is not present. Debugging FCB ProblemsIf your software does not work on Mac OS 9.0 and you suspect you have a dependency on the FCB table, there are a number of ways you can debug it. This section describes how you can search your source code looking for FCB-related bugs and some run-time debugging techniques you can use. Searching Your Source CodeOne obvious way to determine whether your software relies on the format of the FCB table is to run it under Mac OS 9.0. The tricky part, however, is exercising your entire source base. The FCB table dependency may be hiding in a rarely used feature that isn’t exercised by your test suite. A good alternative to testing is to search your source code looking for references to the FCB table. For PowerPC software you should start by running your
program through the Carbon Dater tool (available on the
Carbon
web site). This will flag any references to the FCB table
low-memory accessor routines ( Carbon Dater is not an option for 68K software, so the best thing to do is search your source code looking for some (uncommon) strings that indicate direct access to the FCB table. The strings to look for are:
If you don’t have source for all of your software, you
can also search your 68K code resources for System Error 119If you run your software and the system crashes with a
system error Horrible CrashesIf your application dies with an access exception (or bus error) on Mac OS 9.0, you can look at the program state to determine whether the problem is FCB related. Any of the following in your Processor registers or local variables might indicate FCB-related troubles.
|
Before the BeginningFCBs, as we know them today, were introduced as part of HFS. HFS is built in to all 128K ROMs or later (starting with the Macintosh Plus in 1986), and was available as an extension for 64K ROM computers (the Macintosh 128 and 512). Except for a few corner cases, this technote assumes that you are programming for a 128K ROM system or later. This is a fair assumption because 64K ROM machines do not support System 7.0. Moreover, no current development environment supports development for 64K ROMs. In short, if you are developing for the 64K ROM, you have our sympathy but not our support. |
In the BeginningThis section described the classic FCB table format, which was introduced with the HFS file system and retired in Mac OS 9.0. It also discusses some of the limitations of this format. Classic FCB TableWhen it was introduced, HFS used an simple table to store
FCBs. The table is held in a pointer block in the system
heap and is pointed to by the low-memory global
The format of the classic FCB table is shown in below. A file reference number is the offset into this table of
the corresponding FCB. The file reference number for the
'Nth file is 2 + (N - 1) * FCB Table Design LimitationsThe basic structure of the FCB table implies a limit to
the number of FCBs, and hence the number of simultaneously
open files. The original Macintosh (which was extremely
memory constrained) created a table with 10 FCBs. This
number was derived from the Towards the end of System 6’s lifespan, this limit proved to be a problem for many users. There were two solutions. First, one could use a disk editor (the legendary FEdit, for example) to increase the limit by editing the boot block. Second, one could install the “Up Your FCBs” system extension, which would expand the FCB table to its maximum size at system startup. The maximum size of the classic
FCB table is 32 KB, primarily because a file reference
number is a 16-bit signed offset into the table. This yields
a maximum number of FCBs of (32768 - 2) div
Compatibility NotesThe classic FCB table was never a public data structure. While the format is well known—it is described in Technote 1089, “HFS Elucidations Revisited”—all these descriptions include a warning that relying on this format will cause future compatibility problems. There is, however,
one documented use of the |
System 7.0System 7.0 introduced a number of new file system features related to FCBs. This section describes those features. Remember that all versions of System 7 and Mac OS 8 use the classic FCB table format, and inherit many properties from that format. Parallel FCB TableSystem 7.0 was the first system to track FCB usage by process. When a process opens a file, the FCB is tagged as belonging to that process. If the process quits unexpectedly, the Process Manager automatically closes all the FCBs owned by it. Unfortunately, there was not enough space to store the
Instead, System 7.0 introduced the concept of a parallel FCB table. This table was used to store the PSN for the process that opened the file and, when a process quit, to close all the files that were left open by that process.
Process Manager only tracks files that are opened
synchronously. Files that are opened asynchronous (using
Dynamically Growing FCB TableSystem 7.0 also introduced a mechanism to grow the FCB table dynamically. When a program attempts to open a file
while the FCB table is full, the system returns a
Because of these restrictions, it is still possible to
get a |
System 7.5System 7.5 was the first system to include the File System Manager (FSM) as part of the System file. FSM provides a number of routines which allow you to access FCBs without assuming knowledge of the FCB table format. The four FCB accessor functions are:
These routines are documented in the “Guide to the File System Manager”, which is part of the File System Manager SDK.
These FCB accessor routines were originally intended for
use by FSM plug-ins (and other foreign file systems) but it
is appropriate to use them in other code. However, before
you use these routines you should read the
Concrete Advice section to see
whether you would be better off using File Manager routines
instead (for example, You can test for the availability of these accessors with the following code.
|
Mac OS 8.1Mac OS 8.1 introduced a new, built-in volume format, HFS Plus. Despite significant changes to the internals of the File Manager, Mac OS 8.1 changed the FCB table very little. The most important change was required because HFS Plus
needs more space to store its extents information. The
classic FCB table stores the
first 3 extents of a file in the The solution adopted was to store the extents data for
files on HFS Plus volumes in the
parallel FCB table, leaving
the |
Mac OS 9.0This section describes the features of the Mac OS 9.0 File Manager as they relate to FCBs. It also explains some of the rationale behind the changes made in Mac OS 9.0. It even explains why the new limit to the number of open files is 8,169! Design GoalsMac OS 9.0 includes a significant enhancements to the File Manager, including:
One of the design criteria for the enhanced File Manager was to implement these new features without breaking software that uses documented programming interfaces. Increasing the maximum number of open files required a change to the FCB table format, but this format has never been documented as something that developer can rely on.
GestaltMac OS 9.0 defines a new Big ChangesIn Mac OS 9.0, FCB information is now stored in a private table whose format is undocumented to developers. The information that was previously stored in the
parallel FCB table has been rolled into an expanded FCB. The
expanded FCB is defined by the Mac OS 9.0 also stores iterator control blocks in the FCB table. See Iterator Control Blocks for details. Developers who need immediate access to FCBs must use the FSM accessors. In addition, Mac OS 9.0 introduces more FSM accessor routines, described in the New FSM Accessors section. 68K Code and Low MemoryGiven the massive changes to the FCB table format, it is
clear that the low-memory globals associated with the
classic FCB table format ( Unfortunately, the Apple-supplied glue for
The fake FCB table is pointed to by This layout allows the
PowerPC Code and Low-Memory AccessorsThe case for PowerPC code is somewhat clearer. For
PowerPC code, the implementation of On the other hand, the low-memory accessor routines for
The final decision is:
The rationale for these changes was:
Iterator Control BlocksThe File Manager in Mac OS 9.0 introduces a new
mechanism, the iterator, to find all the items in a
directory or on a volume. An iterator is an
abstract object used to hold the state of a particular bulk
catalog operation. An iterator is described by the
On traditional Mac OS, the state for the
Any software that uses FSM accessors must be careful to
treat iterator control blocks as such, and not to blindly
treat them as FCBs. You can distinguish between an
New FSM AccessorsMac OS 9.0 introduces a number of new FSM utility routines which supplement the routines described earlier. The routines are:
In addition, FCBs are now placed in a search list to speed up the search for open files. The following routines allow an external file system to benefit from the speed gains of this search list.
All of these routines will be further documented in an update to the “Guide to the File System Manager.”
|
SummaryThe format of the FCB table, while documented to developers for illustrative purposes, have never been guaranteed. Mac OS 9.0 changes the format of this table, primarily to increase the maximum number of open files on the system. Apple has made this change such that all existing, documented programming interfaces continue to work. Moreover, the existing FSM accessors (introduced with System 7.5) allow immediate access to an FCB in places where this is necessary. However, Apple strongly recommends that developers avoid immediate access to FCBs and instead use the Carbon-compatible File Manager programming interfaces. |
|
Thanks to Mark Day and Jim Luther.